/*
Section: Robustness checks for IEB analysis
Content: Figures G.2, G.4, Tables G.2, G.3
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/


********************************************************************************
**# LOAD ANALYSIS DATASET
********************************************************************************
	
use "$final/finaldata_main_final_light.dta", replace

********************************************************************************
**# Figure G.2
********************************************************************************
	
eststo clear
local i=1			
foreach out in  unemp_1m  unemp_3m  {
	foreach x in m5y m4y m3y {
	foreach y in 90 {
qui eststo mod`i':  reghdfe `out' treatment_terror_`x'_p90 i.edu_max age frau c.ysm_atunemp##c.ysm_atunemp ln_wage_any_lb ln_firmsize_lb full_time_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb if  register_unemp==1 & k_treatment_terror_`x'_p`y'_st==1, absorb(i.ieb_nat_id#i.year2  i.month#i.year2 i.state )  vce(cluster clusterterror_`x'_p90)
local i=`i'+1
	}
	}
	}
	coefplot mod*,  keep(treatment_terror_*) xline(0, lcolor(cranberry)) ciopts(recast(rspike)) nooffsets aseq swapnames legend(off) graphregion(color(white)) ///
				headings(mod1=`"{bf: Employed within 1m}"' mod4=`"{bf: Employed within 3m}"')		 coeflabels(mod1 = "> average last 5 years" mod2= "> average last 4 years" mod3= "> average  last 3 years" mod4 = "> average last 5 years" mod5= "> average last 4 years" mod6= "> average  last 3 years" mod7 = "> average last 5 years" mod8= "> average last 4 years" mod9= "> average  last 3 years" mod10 = "> average last 5 years" mod11= "> average last 4 years" mod12= "> average  last 3 years")  xscale(r(-0.06(0.02)0.06)) xlabel(-0.06(0.02)0.06)
				
graph save "Graph" "$graph\vary_referencepoint_final.gph", replace
graph export "$graph\vary_referencepoint_final.pdf", as(pdf) name("Graph") replace

********************************************************************************
**# Table G.2: ALTERNATIVE FIXED EFFECTS
*******************************************************************************
global control = "i.edu_max age frau c.ysm_atunemp##c.ysm_atunemp ln_wage_any_lb ln_firmsize full_time_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb"

global statemonth = "i.ieb_nat_id#i.year2 i.year2#i.month i.state#month"
global stateyear = "i.ieb_nat_id#i.year2 i.year2#i.month i.state#year2"

* YSM*ORIGIN
cap recode ysm_atunemp (0/5 = 1) (6/10 = 2) (11/15 = 3) (16/20 = 4) (21/25 = 5) (26/30 = 6) (31/35 = 7) (36/1000 = 8), gen(ysm_atunemp_cat) // generate categorical for years since migration
global ysmorigin = "i.ieb_nat_id#i.year2 i.year2#i.month i.state ysm_atunemp_cat#ieb_nat_id"

* AGE*ORIGIN
cap recode age (0/24 = 1) (25/34 = 2) (35/44 = 3) (45/54 = 4) (55/150 = 5), gen(age_g)
global ageorigin = "i.ieb_nat_id#i.year2 i.year2#i.month i.state age_g#ieb_nat_id"

* SEX*ORIGIN
global sexorigin = "i.ieb_nat_id#i.year2 i.year2#i.month i.state frau#ieb_nat_id"

	
eststo clear 
local i=1
	
	foreach out in unemp_1m unemp_3m  {
	foreach addfe in ysmorigin ageorigin sexorigin {
	qui eststo mod`i': reghdfe `out' treatment_terror_m3y_p90 ${control} if  k_treatment_terror_m3y_p90_st==1 & register_unemp==1 , absorb(${`addfe'})  vce(cluster clusterterror_m3y_p90)
	
	sum	 `out' if k_treatment_terror_m3y_p90_st==1 & register_unemp==1 & treatment_terror_m3y_p90 == 0
	estadd scalar ymean = r(mean)
estadd local yearorigin "Yes"
estadd local yearmonth "Yes"
estadd local state "Yes"
estadd local control "Yes"
estadd local control_ext "Yes"
	estadd local `addfe' "Yes"

	local i=`i'+1
	
		}
		}
		esttab mod* using "$table/iebprepost_main_addfe_final.tex", star(* 0.10 ** 0.05 *** 0.01) se replace b(3) drop(*edu_max age frau *ysm_atunemp ln_wage_any_lb ln_firmsize_lb full_time_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb _cons) nocons  nogaps nonotes noob nomtitle ///
mgroups("Emp. within 1m" "Emp. within 3m", pattern(1 0 0 1 0 0)   prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
varlabel(treatment_terror_m3y_p90 "Post-Terror") scalars( "nobs Observations" "ymean Mean intention to stay"  "yearorigin Origin country x Year FE" "yearmonth Month FE x Year FE"  "state State of Residency FE" "control Indiv. Controls" "control_ext Extended Controls" "ysmorigin Origin c. FE x YSM G. FE " "ageorigin Origin c. FE x Age G. FE" "sexorigin Origin c. FE x Gender FE") sfmt(%9.0fc %9.3f %1s %1s %1s %1s %1s %1s %1s %1s)

	
********************************************************************************
**# Table G.3: ALTERNATIVE SE CLUSTERS
********************************************************************************
	
egen country_year = group(ieb_nat_id year2)

global control = "i.edu_max age frau c.ysm_atunemp##c.ysm_atunemp ln_wage_any_lb ln_firmsize full_time_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb"
	

eststo clear 
local i=1
	
	foreach out in unemp_1m unemp_3m  {

	qui eststo clusterevent_`out': reghdfe `out' treatment_terror_m3y_p90 ${control} if  k_treatment_terror_m3y_p90_st==1 & register_unemp==1 , absorb(i.ieb_nat_id#i.year2 i.year2#i.month i.state)  vce(cluster clusterterror_m3y_p90)
	
	sum	 `out' if k_treatment_terror_m3y_p90_st==1 & register_unemp==1 & treatment_terror_m3y_p90 == 0
	estadd scalar ymean = r(mean)
estadd local yearorigin "Yes"
estadd local yearmonth "Yes"
estadd local state "Yes"
estadd local control "Yes"
estadd local control_ext "Yes"

	
		qui eststo clustercountry_`out': reghdfe `out' treatment_terror_m3y_p90 ${control} if  k_treatment_terror_m3y_p90_st==1 & register_unemp==1 , absorb(i.ieb_nat_id#i.year2 i.year2#i.month i.state)  vce(cluster ieb_nat_id)
	
	sum	 `out' if k_treatment_terror_m3y_p90_st==1 & register_unemp==1 & treatment_terror_m3y_p90 == 0
	estadd scalar ymean = r(mean)
estadd local yearorigin "Yes"
estadd local yearmonth "Yes"
estadd local state "Yes"
estadd local control "Yes"
estadd local control_ext "Yes"


	qui eststo clustercountryyear_`out': reghdfe `out' treatment_terror_m3y_p90 ${control} if  k_treatment_terror_m3y_p90_st==1 & register_unemp==1 , absorb(i.ieb_nat_id#i.year2 i.year2#i.month i.state)  vce(cluster country_year)
	
	sum	 `out' if k_treatment_terror_m3y_p90_st==1 & register_unemp==1 & treatment_terror_m3y_p90 == 0
	estadd scalar ymean = r(mean)
estadd local yearorigin "Yes"
estadd local yearmonth "Yes"
estadd local state "Yes"
estadd local control "Yes"
estadd local control_ext "Yes"

	
		}
		
		esttab *_unemp_1m *_unemp_3m using "$table/iebprepost_main_altse_final.tex", star(* 0.10 ** 0.05 *** 0.01) se replace b(3) drop(*edu_max age frau *ysm_atunemp ln_wage_any_lb ln_firmsize_lb full_time_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb _cons) nocons  nogaps nonotes noob nomtitle ///
mgroups("Emp. within 1m" "Emp. within 3m", pattern(1 0 0 1 0 0)   prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
varlabel(treatment_terror_m3y_p90 "Post-Terror") scalars( "nobs Observations" "ymean Mean intention to stay"  "yearorigin Origin country x Year FE" "yearmonth Month FE x Year FE"  "state State of Residency FE" "control Indiv. Controls" "control_ext Extended Controls" ) sfmt(%9.0fc %9.3f %1s %1s %1s %1s %1s %1s %1s %1s) ///
 mtitles("\shortstack{Event\\level}" "\shortstack{Country of\\Origin}" "\shortstack{Country O.\\x Year}" "\shortstack{Event\\level}" "\shortstack{Country of\\Origin}" "\shortstack{Country O.\\x Year}")

		
********************************************************************************
**# Figures G.4a and G.4c: LEAVE-ONE-OUT WITH YEARS
********************************************************************************		
foreach out in unemp_1m unemp_3m  {
		eststo clear
local i=1
	forvalues i = 2000/2018 {
qui  eststo exclude`i':  reghdfe `out' treatment_terror_m3y_p90 i.edu_max age frau c.ysm_atunemp##c.ysm_atunemp ln_wage_any_lb ln_firmsize if k_treatment_terror_m3y_p90_st==1 & register_unemp==1 & year2 != `i', absorb(i.ieb_nat_id#i.year2  i.month#i.year2 i.state )  vce(cluster clusterterror_m3y_p90)
local i=`i'+1
	}		
      
	coefplot exclude2000 exclude2001 exclude2002 exclude2003 exclude2004 exclude2005 exclude2006 exclude2007 exclude2008 exclude2009 exclude2010 exclude2011 exclude2012 exclude2013 exclude2014 exclude2015 exclude2016 exclude2017 exclude2018, ///
 keep(treatment_terror_m3y_p90) xscale(r(-0.01(0.02)0.04)) xlabel(-0.01(0.02)0.04, nogrid) graphregion(color(white)) nooffsets aseq swapnames legend(off)  ///
 xline(0, lcolor(cranberry) lpattern(solid)) recast(scatter) msize(small) mcolor(navy) ciopts(recast(rspike) color(navy)) /// 
 coeflabels(exclude2000 = "2000" exclude2001 = "2001" exclude2002 = "2002" exclude2003 = "2003" exclude2004 = "2004" exclude2005 = "2005" exclude2006 = "2006" exclude2007 = "2007" exclude2008 = "2008" exclude2009 = "2009" ///
 exclude2010 = "2010" exclude2011 = "2011" exclude2012 = "2012" exclude2013 = "2013" exclude2014 = "2014" exclude2015 = "2015" exclude2016 = "2016" exclude2017 = "2017" exclude2018 = "2018", labsize(small)) xsize(5)
 
	graph save "$graph/ieb_`out'_exclude_year_final.gph", replace
graph export "$graph/ieb_`out'_exclude_year_final.pdf", as(pdf) replace
			}
		

********************************************************************************
**# Figures G.4b and G.4d: LEAVE-ONE-OUT WITH COUNTRIES OF ORIGIN
********************************************************************************		

foreach out in   unemp_1m unemp_3m  {
eststo clear
qui  eststo baseline:  reghdfe `out' treatment_terror_m3y_p90 i.edu_max age frau c.ysm_atunemp##c.ysm_atunemp ln_wage_any_lb ln_firmsize if k_treatment_terror_m3y_p90_st==1 & register_unemp==1 , absorb(i.ieb_nat_id#i.year2  i.month#i.year2 i.state )  vce(cluster clusterterror_m3y_p90)

cap gen sample_ieb = e(sample)
tab ieb_nat_id if sample_ieb == 1

local i=`i'+1
local i=1
foreach  i in 163 134 137 161 129 368 154 152 160 438 252 444 122 144 121 138 {
qui  eststo exclude`i':  reghdfe `out' treatment_terror_m3y_p90 i.edu_max age frau c.ysm_atunemp##c.ysm_atunemp ln_wage_any_lb ln_firmsize if k_treatment_terror_m3y_p90_st==1 & register_unemp==1 & ieb_nat_id != `i', absorb(i.ieb_nat_id#i.year2  i.month#i.year2 i.state )  vce(cluster clusterterror_m3y_p90)
local i=`i'+1
	}		
      
coefplot baseline exclude*, ///
keep(treatment_terror_m3y_p90) xscale(r(-0.01(0.02)0.04)) xlabel(-0.01(0.02)0.04, nogrid) graphregion(color(white)) nooffsets aseq swapnames legend(off)  ///
xline(0, lcolor(red) lpattern(solid)) recast(scatter) msize(small) mcolor(navy) ciopts(recast(rspike) color(navy)) /// 
coeflabels(baseline = "Baseline" exclude163 = "Turkey" exclude134 = "Greece" exclude137 = "Italy" exclude161 = "Spain" exclude129 = "France" exclude368 = "USA" exclude154 = "Romania" exclude152 = "Poland" exclude160 = "Russia" ///
exclude438 = "Iraq" exclude252 = "Morocco" exclude444 = "Kazakhstan" exclude122 = "Bosnia-Herzegovina" exclude144 = "Macedonia" exclude121 = "Albania" exclude133 = "Serbia", labsize(small)) xsize(5)

graph save "$graph/ieb_`out'_exclude_country_final.pdf", replace
graph export "$graph/ieb_`out'_exclude_country_final.pdf", as(pdf) replace
			}
				

clear 
